package com.king.modules.system.controller;

import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.king.common.annotation.Log;
import com.king.common.enums.BusinessType;
import com.king.common.utils.poi.ExcelUtil;
import com.king.framework.base.controller.YyBaseController;
import com.king.framework.base.domain.vo.AjaxResult;
import com.king.framework.base.domain.vo.PageResult;
import com.king.modules.system.domain.SysMenuEntity;
import com.king.modules.system.domain.SysRoleEntity;
import com.king.modules.system.domain.SysUserEntity;
import com.king.modules.system.domain.vo.RoleMenuResForm;
import com.king.modules.system.domain.vo.RoleUsersForm;
import com.king.modules.system.service.SysMenuService;
import com.king.modules.system.service.SysRoleService;
import com.king.modules.system.service.SysUserService;

/**
 * 角色信息
 * 
 * @author king
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends YyBaseController<SysRoleEntity> {
	
	@Autowired
	private SysRoleService roleService;
	
	@Autowired
	private SysUserService userService;

	@Autowired
	private SysMenuService menuService;
	

	@Log(title = "角色管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@sps.hasAuthority('system:role:export')")
	@PostMapping("/export")
	public void export(HttpServletResponse response, SysRoleEntity role) {
		List<SysRoleEntity> list = roleService.selectRoleList(role);
		ExcelUtil<SysRoleEntity> util = new ExcelUtil<SysRoleEntity>(SysRoleEntity.class);
		util.exportExcel(response, list, "角色数据");
	}

	/**
	 * 状态修改
	 */
	@PreAuthorize("@sps.hasAuthority('system:role:edit')")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@PutMapping("/changeStatus")
	public AjaxResult changeStatus(@RequestBody SysRoleEntity role) {
		roleService.checkRoleAllowed(role);
		roleService.checkRoleDataScope(role.getId());
		role.setMr(getUserId());
		return toAjax(roleService.updateRoleStatus(role));
	}


	/**
	 * 查询已分配用户角色列表
	 */
	@PreAuthorize("@sps.hasAuthority('system:role:query')")
	@PostMapping("/inSelectUsers")
	public AjaxResult inSelectUsers(@RequestBody SysUserEntity user) {
		startPage();
		List<SysUserEntity> list = userService.inSelectRoleUsers(user);
		return success( new PageResult<SysUserEntity>(list));
	}

	/**
	 * 查询未分配用户角色列表
	 */
	@PreAuthorize("@sps.hasAuthority('system:role:query')")
	@PostMapping("/unSelectUsers")
	public AjaxResult unSelectUsers(@RequestBody SysUserEntity user) {
		startPage();
		List<SysUserEntity> list = userService.unSelectRoleUsers(user);
		return success( new PageResult<SysUserEntity>(list));
	}


	/**
	 * 批量取消授权用户
	 */
	@PreAuthorize("@sps.hasAuthority('system:role:edit')")
	@Log(title = "角色管理", businessType = BusinessType.GRANT)
	@PostMapping("/cancelUsers")
	public AjaxResult cancelUsers(@RequestBody RoleUsersForm roleUsers) {
		return toAjax(roleService.deleteAuthUsers(roleUsers.getRoleId(), roleUsers.getUserIds()));
	}

	/**
	 * 批量选择用户授权
	 */
	@PreAuthorize("@sps.hasAuthority('system:role:edit')")
	@Log(title = "角色管理", businessType = BusinessType.GRANT)
	@PostMapping("/selectUsers")
	public AjaxResult selectUsers(@RequestBody RoleUsersForm roleUsers) {
		roleService.checkRoleDataScope(roleUsers.getRoleId());
		return toAjax(roleService.insertAuthUsers(roleUsers.getRoleId(), roleUsers.getUserIds()));
	}


	
	
	/**
	 * 查询角色的菜单
	 * @param roleId
	 * @return
	 */
	@PostMapping(value = "/queryMenuRes/{roleId}")
	public AjaxResult queryMenuRes(@PathVariable("roleId") Long roleId) {
		List<SysMenuEntity> menus = menuService.selectGroupMenuList(getLoginUser());
		List<Long> menuIds = menuService.selectMenuIdsByRoleId(roleId);
		Set<Long> menuIdSet = menuIds.stream().map(Function.identity()).collect(Collectors.toSet());
		for(SysMenuEntity menu : menus){
			if(menuIdSet.contains(menu.getId())){
				menu.setChecked(true);
			}
		}
		PageResult<SysMenuEntity> datas = new PageResult<>(menus);
        return success(datas);
	}
	
	/**
	 * 角色授权菜单
	 * @param form
	 * @return
	 */
	@PostMapping(value = "/authMenuRes")
	public AjaxResult authMenuRes(@RequestBody RoleMenuResForm form) {
		roleService.authMenuRes(form);
		return success();
	}
}
